Conditions | 1 |
Paths | 49 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
59 | inji.onLoad(function () { |
||
60 | |||
61 | //plugin bootstrap minus and plus |
||
62 | //http://jsfiddle.net/laelitenetwork/puJ6G/ |
||
63 | $('body').on('click', '.btn-number', function (e) { |
||
64 | e.preventDefault(); |
||
65 | |||
66 | var fieldName = $(this).data('field'); |
||
67 | var type = $(this).data('type'); |
||
68 | var input = $("input[name='" + fieldName + "']"); |
||
69 | var currentVal = parseFloat(input.val()); |
||
70 | if (!isNaN(currentVal)) { |
||
71 | if (type == 'minus') { |
||
72 | |||
73 | if (currentVal > input.attr('min')) { |
||
74 | input.val(currentVal - 1).change(); |
||
75 | } |
||
76 | if (parseFloat(input.val()) == input.attr('min')) { |
||
77 | $(this).attr('disabled', true); |
||
78 | } |
||
79 | |||
80 | } else if (type == 'plus') { |
||
81 | |||
82 | if (currentVal < input.attr('max')) { |
||
83 | input.val(currentVal + 1).change(); |
||
84 | } |
||
85 | if (parseFloat(input.val()) == input.attr('max')) { |
||
86 | $(this).attr('disabled', true); |
||
87 | } |
||
88 | |||
89 | } |
||
90 | } else { |
||
91 | input.val(0); |
||
92 | } |
||
93 | }); |
||
94 | $('body').on('focusin', '.input-number', function () { |
||
95 | $(this).data('oldValue', $(this).val()); |
||
96 | }); |
||
97 | $('body').on('change', '.input-number', function () { |
||
98 | |||
99 | var minValue = parseFloat($(this).attr('min')); |
||
100 | var maxValue = parseFloat($(this).attr('max')); |
||
101 | var valueCurrent = parseFloat($(this).val()); |
||
102 | |||
103 | var name = $(this).attr('name'); |
||
104 | if (valueCurrent >= minValue) { |
||
105 | $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled') |
||
106 | } else { |
||
107 | alert('Нельзя заказать меньше ' + minValue); |
||
108 | $(this).val($(this).data('oldValue')); |
||
109 | } |
||
110 | if (valueCurrent <= maxValue) { |
||
111 | $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled') |
||
112 | } else { |
||
113 | alert('Извините, но больше нету'); |
||
114 | $(this).val($(this).data('oldValue')); |
||
115 | } |
||
116 | |||
117 | |||
118 | }); |
||
119 | |||
120 | $('body').on('keydown', ".input-number", function (e) { |
||
121 | // Allow: backspace, delete, tab, escape, enter and . |
||
122 | if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 || |
||
123 | // Allow: Ctrl+A |
||
124 | (e.keyCode == 65 && e.ctrlKey === true) || |
||
125 | // Allow: home, end, left, right |
||
126 | (e.keyCode >= 35 && e.keyCode <= 39)) { |
||
127 | // let it happen, don't do anything |
||
128 | return; |
||
129 | } |
||
130 | // Ensure that it is a number and stop the keypress |
||
131 | if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { |
||
132 | e.preventDefault(); |
||
133 | } |
||
134 | }); |
||
135 | |||
136 | }) |
||
137 |